home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 26 / AMIGAplus Sonderheft 26 (2000)(Falke)(DE)(Track 1 of 2)[!].iso / Tools / Packer / PPCUnACE / Src / uac_crt.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  4KB  |  145 lines

  1. /* ------------------------------------------------------------------------ */
  2. /*                                                                          */
  3. /*      Creates/Replaces files or directories.                              */
  4. /*                                                                          */
  5. /* ------------------------------------------------------------------------ */
  6.  
  7. #include "os.h"
  8.  
  9. #include <fcntl.h>     // AMIGA: open()
  10. #include <stdio.h>     // printf() remove()
  11. #include <string.h>    // strncpy()
  12. #include <sys/stat.h>  // struct stat
  13.  
  14. #ifdef UNIX
  15. #include <unistd.h>
  16. #else
  17.  #if !defined(VA_CPP)
  18.   #include <dos.h>     // AMIGA: mkdir() rmdir() DOS: _dos_*()
  19.  #endif
  20. #endif
  21. #if defined(DOS) || defined(WINNT) || defined(WIN16) || defined(VA_CPP)
  22.  #include <io.h>       // access()
  23. #if defined(WATCOM_C) || defined(VA_CPP)
  24.  #include <direct.h>   // mkdir()
  25. #else
  26.  #include <dir.h>      // mkdir()
  27. #endif
  28. #endif
  29.  
  30. #include "globals.h"
  31. #include "uac_crt.h"
  32. #include "uac_sys.h"
  33.  
  34. /* gets file name from header
  35.  */
  36. CHAR *ace_fname(CHAR * s, thead * head, INT nopath)
  37. {
  38.    INT  i;
  39.    char *cp;
  40.  
  41.    strncpy(s, (*(tfhead *) head).FNAME, i = (*(tfhead *) head).FNAME_SIZE);
  42.    s[i] = 0;
  43.  
  44.    if (nopath)
  45.    {
  46.       cp=strrchr(s, '\\');
  47.       if (cp)
  48.          memmove(s, cp+1, strlen(cp));
  49.    }
  50. #if (DIRSEP!='\\')                  // replace msdos directory seperator
  51.    else
  52.    {                                // by current OS seperator
  53.       cp=s;
  54.       while ((cp=strchr(cp, '\\'))!=NULL)
  55.          *cp++=DIRSEP;
  56.    }
  57. #endif
  58.  
  59.    return s;
  60. }
  61.  
  62. void check_ext_dir(CHAR * f)        // checks/creates path of file
  63. {
  64.    CHAR *cp,
  65.         d[PATH_MAX];
  66.    INT  i;
  67.  
  68.    d[0] = 0;
  69.  
  70.    for (;;)
  71.    {
  72.       if ((cp = (CHAR *) strchr(&f[strlen(d) + 1], DIRSEP))!=NULL)
  73.       {
  74.          i = cp - f;
  75.          strncpy(d, f, i);
  76.          d[i] = 0;
  77.       }
  78.       else
  79.          return;
  80.  
  81.       if (!fileexists(d))
  82.          if (mkdir(d))
  83.          {
  84.             f_err = ERR_WRITE;
  85.             printf("\n    Error while creating directory.\n");
  86.          }
  87.    }
  88. }
  89.  
  90. INT  ovr_delete(CHAR * n)           // deletes directory or file
  91. {
  92.    if (remove(n) && rmdir(n))
  93.    {
  94.       printf("\n    Could not delete file or directory. Access denied.\n");
  95.       return (1);
  96.    }
  97.    return (0);
  98. }
  99.  
  100. INT  create_dest_file(CHAR * file, INT a)  // creates file or directory
  101. {
  102.    INT  han,
  103.         i  = 0,
  104.         ex = fileexists(file);
  105.    struct stat st;
  106.  
  107.    check_ext_dir(file);
  108.    if (f_err)
  109.       return (-1);
  110.    if (a & _A_SUBDIR)
  111.    {                                // create dir or file?
  112.       if (ex) stat(file, &st);
  113.       if (ex ? (st.st_mode & S_IFDIR) : mkdir(file))
  114.       {
  115.          printf("\n    Could not create directory.\n");
  116.          return (-1);
  117.       }
  118. #ifdef DOS
  119.       _dos_setfileattr(file, a);    // set directory attributes
  120. #endif
  121.       return (-1);
  122.    }
  123.    else
  124.    {
  125.       if (ex)
  126.       {                             // does the file already exist
  127.          if (!f_ovrall)
  128.          {
  129.             i = wrask("Overwrite existing file?");  // prompt for overwrite
  130.             f_ovrall = (i == 1);
  131.             if (i == 3)
  132.                f_err = ERR_USER;
  133.          }
  134.          if ((i && !f_ovrall) || ovr_delete(file))
  135.             return (-1);            // delete?
  136.       }
  137.       if ((han = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
  138.                             S_IREAD | S_IWRITE | S_IEXEC | S_IDELETE |
  139.                             S_IRGRP | S_IWGRP  | S_IROTH | S_IWOTH )) < 0)
  140.          printf("\n    Could not create destination file.\n");
  141.       return (han);
  142.    }
  143. }
  144.  
  145.